﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MegavaniaX.World;
using MegavaniaX.Messaging.Events;

namespace MegavaniaX.Actors.States
{
    public class PlayerStateClimb : PlayerStateBase
    {
        private float _startingX;
        private float _startingY;

        private static MegavaniaEventType[] _eventsAllowed = new MegavaniaEventType[]
        {
            MegavaniaEventType.JumpPressed, // temporarily just up for now
            MegavaniaEventType.ShootPressed,
            MegavaniaEventType.MoveRightPressed,
            MegavaniaEventType.MoveLeftPressed
        };      

        public PlayerStateClimb(Player player, float startingX, float startingY)
            : base(player)
        {
            _startingX = startingX;
            _startingY = startingY;
        }


        public override void OnEnter()
        {
            _player.position.X = _startingX;
            _player.position.Y = _startingY;
            _player.velocity = Microsoft.Xna.Framework.Vector2.Zero;
            _player.CurrentAnimation = _player.animationClimb;
        }

        public override bool Update(TimeSpan elapsedTime, Screen screen)
        {
            //if (base.Update(elapsedTime, screen))
            //    return true;
            _player.position.Y += _player.velocity.Y * elapsedTime.Milliseconds;
            CheckTransitions(screen);

            return false;

        }

        public override bool CanHandleEvent(MegavaniaEventType evtType)
        {
            return _eventsAllowed.Contains(evtType);
        }

        public override bool HandleEvent(IMegavaniaEvent evt)
        {            
            if (evt.EventType == MegavaniaEventType.JumpPressed)
            {
                _player.velocity.Y = Player.ClimbSpeed;
                return false;
            }
            if (evt.EventType == MegavaniaEventType.MoveRightPressed || evt.EventType == MegavaniaEventType.MoveLeftPressed)
            {
                if (evt.EventType == MegavaniaEventType.MoveRightPressed)
                    _player.velocity.X = Player.InitialXVelo;
                else
                    _player.velocity.X = -Player.InitialXVelo;
                NextState = new PlayerStateJump(_player, 0.0f);
                return true;
            }

            return false;
        }

        public override void StopShootingAnimation()
        {
            _player.CurrentAnimation = _player.animationClimb;
        }

        public override void OnExit()
        {

        }
    }
}
